﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package eu.orangeflash.webapis.xmlrpc {
    import flash.net.*;
    import eu.orangeflash.webapis.xmlrpc.utils.*;
    import eu.orangeflash.webapis.events.*;
    import flash.events.*;

    public class MethodCall extends EventDispatcher implements IMethodCall {

        protected var name:String;
        protected var serializer:IXMLRPCSerializer;
        protected var urlRequest:URLRequest;
        protected var methodResponse:IMethodResponse;
        protected var deserializer:IXMLRPCDeserializer;
        protected var args:Array;
        protected var serviceClient:IClient;
        protected var urlLoader:URLLoader;

        public function MethodCall(client:IClient){
            urlLoader = new URLLoader();
            urlRequest = new URLRequest();
            super();
            this.client = client;
            configureHandlers();
            serializer = new Serializer();
            deserializer = new Deserializer();
        }
        public function set params(value:Array):void{
            args = value;
        }
        public function get params():Array{
            return (args);
        }
        public function set methodName(value:String):void{
            name = value;
        }
        public function get methodName():String{
            return (name);
        }
        public function get data():Object{
            return (urlLoader.data);
        }
        public function get result():Array{
            return (methodResponse.params);
        }
        public function set client(value:IClient):void{
            serviceClient = value;
            urlRequest.url = value.serviceURL;
            urlRequest.method = URLRequestMethod.POST;
            urlRequest.contentType = "text/xml";
        }
        protected function onComplete(event:Event):void{
            methodResponse = deserializer.createResponse(XML(urlLoader.data));
            if (response.isFault){
                dispatchEvent(new XMLRPCEvent(XMLRPCEvent.FAULT, client, this));
                return;
            };
            dispatchEvent(new XMLRPCEvent(XMLRPCEvent.RESULT, client, this));
        }
        public function get response():IMethodResponse{
            return (methodResponse);
        }
        public function get client():IClient{
            return (serviceClient);
        }
        protected function configureHandlers():void{
            urlLoader.addEventListener(Event.COMPLETE, onComplete);
        }
        public function invoke(methodName:String=null, params:Array=null):void{
            methodName = ((methodName)==null) ? this.methodName : methodName;
            params = ((params)==null) ? this.params : params;
            urlRequest.data = serializer.createMethod(methodName, params);
            urlLoader.load(urlRequest);
            dispatchEvent(new XMLRPCEvent(XMLRPCEvent.INVOKE, client, this));
        }

    }
}//package eu.orangeflash.webapis.xmlrpc 
